package controllers.customer;

import java.util.Collection;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.CustomerService;
import services.GalleryService;
import services.PaintingService;
import controllers.AbstractController;
import domain.Gallery;
import domain.Painting;
import forms.PaintingForm;


@Controller
@RequestMapping("/painting/customer")
public class PaintingCustomerController extends AbstractController {

	// Services
	@Autowired
	private PaintingService paintingService;
	@Autowired
	private GalleryService galleryService;
	@Autowired
	private CustomerService customerService;

	// Constructor
	public PaintingCustomerController() {
		super();
	}

	// Listing
	@RequestMapping(value = "/ownedList", method = RequestMethod.GET)
	public ModelAndView listCustomer() {
		ModelAndView result;
		Collection<Painting> paintings;
		Boolean hasActiveAuction;
		hasActiveAuction = false;
		Integer paintingId = null;
		paintings = paintingService.findOwnedPaintings();

		result = new ModelAndView("painting/ownedList");
		result.addObject("requestURI", "painting/customer/ownedList.do");
		result.addObject("paintings", paintings);
		result.addObject("hasActiveAuction", hasActiveAuction);
		result.addObject("paintingId", paintingId);
		result.addObject("backURI", "/welcome/index.do");
		return result;
	}
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView listAll() {
		ModelAndView result;
		Collection<Painting> paintings;
		Boolean hasActiveAuction;
		hasActiveAuction = false;
		Integer paintingId = null;
		paintings = paintingService.findAll();

		result = new ModelAndView("painting/existingList");
		result.addObject("requestURI", "painting/customer/list.do");
		result.addObject("paintings", paintings);
		result.addObject("hasActiveAuction", hasActiveAuction);
		result.addObject("paintingId", paintingId);
		result.addObject("backURI", "/welcome/index.do");
		return result;
	}
	
	@RequestMapping(value = "/galleryList", method = RequestMethod.GET)
	public ModelAndView list(@RequestParam int galleryId) {
		ModelAndView result;
		Gallery gallery;
		Collection<Painting> paintings;
		Boolean notCreate;

		gallery = galleryService.findOne(galleryId);
		paintings = paintingService.findByGallery(gallery);
		notCreate = true;

		result = new ModelAndView("painting/galleryList");
		result.addObject("requestURI", "painting/customer/galleryList.do?=" + galleryId);
		result.addObject("paintings", paintings);
		result.addObject("backURI", "gallery/customer/edit.do?galleryId=" + galleryId);
		result.addObject("notCreate", notCreate);
		result.addObject("galleryId", galleryId);
		return result;
	}

	// Creation
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public ModelAndView create() {
		ModelAndView result;
		PaintingForm paintingForm;

		Boolean isNew;
		Boolean editable;

		paintingForm = paintingService.createForm();
		isNew = true;
		editable = true;

		result = new ModelAndView("painting/create");
		result.addObject("paintingForm", paintingForm);
		result.addObject("requestURI", "painting/customer/edit.do");
		result.addObject("isNew", isNew);
		result.addObject("editable", editable);
		result.addObject("backURI", "/welcome/index.do");

		return result;

	}

	// Edition
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public ModelAndView edit(@RequestParam int paintingId) {
		ModelAndView result;
		PaintingForm paintingForm;
		Painting painting;
		Collection<Gallery> galleries;
		Boolean isNew;
		Boolean editable;
		Boolean noPendingAuction;

		galleries = galleryService.findByCustomer(customerService.findByPrincipal());
		painting = paintingService.findOne(paintingId);
		paintingForm = paintingService.paintingToForm(painting);

		isNew = false;
		editable = paintingService.noActiveAuctions(painting);
		noPendingAuction = paintingService.noPendingAuctions(painting);

		result = new ModelAndView("painting/edit");
		result.addObject("paintingForm", paintingForm);
		result.addObject("paintingId", paintingId);
		result.addObject("requestURI", "painting/customer/edit.do");
		result.addObject("galleries", galleries);
		result.addObject("isNew", isNew);
		result.addObject("editable", editable);
		result.addObject("noPendingAuction", noPendingAuction);
		result.addObject("backURI", "/painting/customer/ownedList.do");

		return result;

	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST, params="save")
	public ModelAndView save(@Valid PaintingForm paintingForm,
			BindingResult binding) {
		ModelAndView result;
		Painting painting;
		Boolean isNew;
		Boolean editable;

		if (binding.hasErrors()) {
			result = createEditModelAndView(paintingForm);
		} else {
			try {
				painting = paintingService.reconstruct(paintingForm);
				paintingService.save(painting);
				result = new ModelAndView("redirect:../../");

			} catch (Throwable oops) {

				result = createEditModelAndView(paintingForm,
						"painting.commit.error");
			}
		}

		isNew = paintingForm.getPaintingId() == 0;
		editable = true;

		result.addObject("requestURI", "painting/customer/edit.do");
		result.addObject("isNew", isNew);
		result.addObject("editable", editable);
		result.addObject("backURI", "/welcome/index.do");

		return result;
	}

	// Ancillary methods
	protected ModelAndView createEditModelAndView(PaintingForm paintingForm) {
		ModelAndView result;

		result = createEditModelAndView(paintingForm, null);

		return result;
	}

	protected ModelAndView createEditModelAndView(PaintingForm paintingForm,
			String message) {
		ModelAndView result;

		result = new ModelAndView("painting/edit");

		result.addObject("paintingForm", paintingForm);
		result.addObject("message", message);

		return result;
	}
}
